package com.beone.smart.smartBeauty.utils;

import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.github.eugeneheen.berry.kit.security.JwtKit;
import com.github.eugeneheen.berry.kit.security.token.JwtToken;
import com.github.eugeneheen.berry.kit.security.token.JwtTokenBuilder;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

public class JwtUtils {

    public static String getUserId(String token) {
        JwtToken jwtToken = new JwtKit().decode(token);
        String uid = jwtToken.getId();
        if (StringUtils.isEmpty(uid)) {
            throw new GlobalException(ResultTool.HttpResponseCode.UNAUTHORIZED, "token解析失败，请重新登录");
        }

        return uid;
    }

    public static JwtToken getUser(String token) {
        JwtToken jwtToken = new JwtKit().decode(token);
        String uid = jwtToken.getId();
        if (StringUtils.isEmpty(uid)) {
            throw new GlobalException(ResultTool.HttpResponseCode.UNAUTHORIZED, "token解析失败，请重新登录");
        }
        return jwtToken;
    }

    //ab821f66e8f788a424a58b0cc654985e
    public static void main(String[] args) {
        JwtToken jwtToken = JwtTokenBuilder.newJwtTokenBuilder("ab821f66e8f788a424a58b0cc654985e", "login", "login")
                .property("userId", "ab821f66e8f788a424a58b0cc654985e")
                .expirationInMinutes(3000).build();
        JwtKit jwtKit = new JwtKit();
        System.out.println(jwtKit.encode(jwtToken));
        Assert.notNull(jwtToken, "Source must not be null");
    }
}
